07. Exercise: Set MyCanvasView as the Content View

22 06 AAK SetContentView SC V2

Exercise

In this exercise you are going to Set MyCanvasView as the content view.

  1. Open strings.xml and define a string to use for the view's content description.
<string name="canvasContentDescription">Mini Paint is a simple line drawing app.
   Drag your fingers to draw. Rotate the phone to clear.</string>
  1. Open MainActivity.kt.

  2. In onCreate(), delete setContentView(R.layout.activity_main).

  3. Create an instance of MyCanvasView.

val myCanvasView = MyCanvasView(this)
  1. Below that, request the full screen for the layout of myCanvasView. Do this by setting the SYSTEM_UI_FLAG_FULLSCREEN flag on myCanvasView. In this way, the view completely fills the screen.
myCanvasView.systemUiVisibility = SYSTEM_UI_FLAG_FULLSCREEN
  1. Add a content description.
myCanvasView.contentDescription = getString(R.string.canvasContentDescription)
  1. Below that, set the content view to myCanvasView.
setContentView(myCanvasView)
  1. Run your app.

  2. You will see a completely white screen, because the canvas has no size, and you have not drawn anything yet.

Note: You will need to know the size of the view for drawing, but you cannot get the size of the view in the onCreate() method, because the size has not been determined at this point.